/**
 * 向特定类型的客户端广播消息
 * @param string $type
 * @param array $data
 */
protected function broadcastToType(string $type, array $data)
{
    try {
        // 确保数据是有效的数组且有type字段
        if (!is_array($data) || !isset($data['type'])) {
            echo "警告: 尝试广播无效数据\n";
            return;
        }
        
        // 添加服务器时间戳，帮助客户端识别消息新鲜度
        $data['server_time'] = time();
        $data['broadcast_id'] = uniqid('bcast_');
        
        $message = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        
        // 检查JSON编码是否成功
        if ($message === false) {
            echo "JSON编码失败: " . json_last_error_msg() . "\n";
            return;
        }
        
        $count = 0;
        $errors = 0;
        
        foreach ($this->clients as $client) {
            $clientId = $client->resourceId;
            $clientType = $this->clientTypes[$clientId] ?? 'unknown';
            
            if ($clientType === $type) {
                try {
                    $client->send($message);
                    $count++;
                    echo "成功发送消息到 {$type} 客户端 {$clientId}\n";
                } catch (\Exception $e) {
                    $errors++;
                    echo "向客户端 {$clientId} 发送消息失败: " . $e->getMessage() . "\n";
                }
            }
        }
        
        echo "消息已广播给 {$count} 个 {$type} 类型客户端 (失败: {$errors})\n";
    } catch (\Exception $e) {
        echo "广播消息时出错: " . $e->getMessage() . "\n";
    }
} 